const {param} = require('express-validator')
const validate = require('../middleware/validate')
const Article = require('../models/Article')
const User = require('../models/User')
exports.postFavorite = validate([
  param('articleId').notEmpty().withMessage('收藏文章ID不能为空'),
  param('articleId').isMongoId().withMessage('收藏文章ID格式不正确'),
  param('articleId').custom(async (id) => {
    console.log('id:', id)
    try {
      const article = await Article.findById(id)
      if (!article) {
        return Promise.reject('收藏文章不存在')
      }
    } catch (error) {
      Promise.reject('收藏文章不存在')
    }
    return true
  }),
])
exports.getFavorites = validate([
  param('userId').notEmpty().withMessage('用户ID不能为空'),
  param('userId').isMongoId().withMessage('用户ID格式不正确'),
  param('userId').custom(async (id) => {
    try {
      const user = await User.findById(id)
      if (!user) {
        return Promise.reject('收藏文章不存在')
      }
    } catch (error) {
      Promise.reject('收藏文章不存在')
    }
    return true
  }),
])
